package graph;

import graph.entities.EntityType;
import graph.scenes.Scene;

import java.util.LinkedList;
import java.util.List;

import ogl.app.Input;
import ogl.app.InputXbox;
import ogl.app.InputXbox.ThumbPosition;
import ogl.vecmath.Vector;

import org.lwjgl.input.Keyboard;

public class Entities {
	private Scene scene;
	private List<Entity> entities;
	private float camSwitchTime;
	private float lightSwitchTime;
	private boolean jump = false;
	private float jumpDuration = 0;

	public Entities(Scene scene, List<Entity> entities) {
		this.scene = scene;
		this.entities = entities;
	}

	public Entities(Scene scene) {
		this.scene = scene;
		this.entities = new LinkedList<>();
	}

	public void addEntity(Entity e) {
		entities.add(e);
	}

	public void removeEntity(Entity e) {
		entities.remove(e);
	}

	public void simulateAllEntity(float elapsed, Input input) {
		simluateMouseControls(elapsed, input);
		simulateKeyboardControls(elapsed, input);
		simulateXBoxControls(elapsed, input);
	}

	private void simluateMouseControls(float elapsed, Input input) {
		Vector mouse = input.getNormalizedMousePosition();
		float x = mouse.x();
		float y = mouse.y();

		if (x > 0.1f) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotateCamRight, -x);
			}
		} else if (x < -0.1f) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotateCamLeft, x);
			}
		}
		if (y > 0.3f) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotateCamUp, -y);
			}
		} else if (y < -0.3f) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotateCamDown, y);
			}
		}
	}

	private void simulateKeyboardControls(float elapsed, Input input) {
		// Rotation
		if (input.isKeyDown(Keyboard.KEY_RIGHT)) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotationY, elapsed);
			}
		}
		if (input.isKeyDown(Keyboard.KEY_DOWN)) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotationX, elapsed);
			}
		}
		if (input.isKeyDown(Keyboard.KEY_LEFT)) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotationMY, elapsed);
			}
		}
		if (input.isKeyDown(Keyboard.KEY_UP)) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotationMX, elapsed);
			}
		}

		// WASD
		if (input.isKeyDown(Keyboard.KEY_W)) {
			for (Entity e : entities) {
				e.simulate(EntityType.WalkForward, elapsed);
			}
		}
		if (input.isKeyDown(Keyboard.KEY_A)) {
			for (Entity e : entities) {
				e.simulate(EntityType.WalkLeft, elapsed);
			}
		}
		if (input.isKeyDown(Keyboard.KEY_S)) {
			for (Entity e : entities) {
				e.simulate(EntityType.WalkBackward, elapsed);
			}
		}
		if (input.isKeyDown(Keyboard.KEY_D)) {
			for (Entity e : entities) {
				e.simulate(EntityType.WalkRight, elapsed);
			}
		}

		// Skalierung
		if (input.isKeyDown(Keyboard.KEY_ADD)) {
			for (Entity e : entities) {
				e.simulate(EntityType.ScalePlus, elapsed);
			}
		}
		if (input.isKeyDown(Keyboard.KEY_MINUS)) {
			for (Entity e : entities) {
				e.simulate(EntityType.ScaleMinus, elapsed);
			}
		}
		
		// Jump
		if (input.isKeyDown(Keyboard.KEY_SPACE)) {
			jumpDuration += elapsed;
			for (Entity e : entities) {
				e.simulate(EntityType.JumpUp, jumpDuration);
			}
		}
		
		if (jumpDuration > 0 && jumpDuration < 1) {
			jumpDuration += elapsed;
			for (Entity e : entities) {
				e.simulate(EntityType.JumpUp, jumpDuration);
			}
		} else {
			jumpDuration = 0;
		}
		

		// Camera Switch
		if (camSwitchTime != 0)
			camSwitchTime += elapsed;
		if (camSwitchTime > 0.5f)
			camSwitchTime = 0;

		if (input.isKeyDown(Keyboard.KEY_0)) {
			if (camSwitchTime == 0) {
				scene.switchCameras();
				System.out.println("Switch");
				camSwitchTime += elapsed;
			}
		}

		// Light Switch
		if (lightSwitchTime != 0)
			lightSwitchTime += elapsed;
		if (lightSwitchTime > 0.5f)
			lightSwitchTime = 0;

		if (input.isKeyDown(Keyboard.KEY_9)) {
			if (lightSwitchTime == 0) {
				scene.switchLight();
				lightSwitchTime += elapsed;
			}
		}
	}

	private void simulateXBoxControls(float elapsed, Input input) {
		if (input.isKeyDown(InputXbox.KEY_A)) {
			System.out.println("Juuuuump");
		}

		// Walking
		ThumbPosition leftThumb = input.getLeftThumbPosition();
		float angleR = leftThumb.getAngle();
		float positionR = (float) (leftThumb.getPosition() / 4.5);

		if (positionR > 0.02f) {
			// forward
			if ((angleR > 315 && angleR < 360) || (angleR >= 0 && angleR < 45)) {
				for (Entity e : entities) {
					e.simulate(EntityType.WalkForward, positionR);
				}
			}
			// left
			if (angleR > 235 && angleR < 315) {
				for (Entity e : entities) {
					e.simulate(EntityType.WalkLeft, positionR);
				}
			}
			// backward
			if (angleR > 135 && angleR < 235) {
				for (Entity e : entities) {
					e.simulate(EntityType.WalkBackward, positionR);
				}
			}
			// right
			if (angleR > 45 && angleR < 135) {
				for (Entity e : entities) {
					e.simulate(EntityType.WalkRight, positionR);
				}
			}
		}

		// Camera
		ThumbPosition rightThumb = input.getRightThumbPosition();
		float angleL = rightThumb.getAngle();
		float positionL = rightThumb.getPosition();

		if (angleL > 0 && angleL < 180) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotateCamRight, -positionL);
			}
		} else if (angleL > 180 && angleL < 360) {
			for (Entity e : entities) {
				e.simulate(EntityType.RotateCamLeft, -positionL);
			}
		}

	}
}
